/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display.miniboard;

import deatax.Main;
import deatax.ResourceLoader;
import deatax.display.Texture;
import deatax.display.Tween;
import deatax.display.TweenListener;
import java.io.File;
import javax.media.opengl.GL2;

/**
 *
 * @author Caroline
 */
public class CardRenderer implements TweenListener {

    Texture front;
    Texture back;
    Tween tween;
    Tween scale;
    Tween rotate;
    public static final Texture DEFAULT_BACK = new Texture("card/death-back.png");
    public static final Texture DEFAULT_FRONT = new Texture("card/death-front.png");

    public CardRenderer(String back, String front, MiniBoardTile parent) {
        front = front.toLowerCase();
        back = back.toLowerCase();
//        File f = new File("card/" + front + ".png");
//        if (f.exists()) {
//            this.front = new Texture("card/" + front + ".png");
//        } else {
//            System.out.println("card/" + front + ".png - not found");
//            this.front = DEFAULT_FRONT;
//        }
//        f = new File("card/" + front + ".png");
//        if (f.exists()) {
//            this.back = new Texture("card/" + back + "-back.png");
//        } else {
//            this.back = DEFAULT_BACK;
//        }
        if (ResourceLoader.resourceExists("card/" + front + ".png")) {
            this.front = new Texture("card/" + front + ".png");
        } else {
            this.front = DEFAULT_FRONT;
        }
        if (ResourceLoader.resourceExists("card/" + back + "-back.png")) {
            this.back = new Texture("card/" + back + "-back.png");
        } else {
            this.back = DEFAULT_BACK;
        }
        

        tween = new Tween(parent.getCoords());
        tween.accel = 0.1f;
        tween.max_speed = 1f;
        tween.setTarget(0, -7, 12);
        System.out.println(tween);

        scale = new Tween(0.001f, 0.001f, 0.001f);
        scale.setTarget(3, 3, 3);
        scale.accel = 0.0001f;

        rotate = new Tween(0, 0, 2000);
        rotate.setTarget(50, 0, 0);
        rotate.accel = 1f;
        rotate.max_speed = 1f;

        rotate.setListener(this);
    }

    public void draw(GL2 gl) {
        tween.step();
        scale.step();
        rotate.step();
//        System.out.println(tween.toString());


        gl.glColor3f(1, 1, 1);
        gl.glEnable(GL2.GL_CULL_FACE);
        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glEnable(GL2.GL_BLEND);

        back.bind(gl);
        gl.glPushMatrix();
        tween.translate(gl);
        scale.scale(gl);
        rotate.rotate(gl);
        gl.glBegin(GL2.GL_TRIANGLE_FAN);
        for (int i = 6; i > -1; i -= 2) {
            gl.glTexCoord2f((CardStackRenderer.vertices[i] + 1) / 2, -(CardStackRenderer.vertices[i + 1] + 1) / 2);
            gl.glVertex2f(CardStackRenderer.vertices[i] * CardStackRenderer.CARD_WIDTH, CardStackRenderer.vertices[i + 1] * CardStackRenderer.CARD_HEIGHT);
        }
        gl.glEnd();
        front.bind(gl);
        gl.glBegin(GL2.GL_TRIANGLE_FAN);
        for (int i = 0; i < 8; i += 2) {
            gl.glTexCoord2f(-(CardStackRenderer.vertices[i] + 1) / 2, -(CardStackRenderer.vertices[i + 1] + 1) / 2);
            gl.glVertex2f(CardStackRenderer.vertices[i] * CardStackRenderer.CARD_WIDTH, CardStackRenderer.vertices[i + 1] * CardStackRenderer.CARD_HEIGHT);
        }
        gl.glEnd();
        gl.glPopMatrix();

        gl.glDisable(GL2.GL_TEXTURE_2D);
    }

    @Override
    public void onTweenFinished(Tween t) {
        if (rotate.getCurrent().x == 50) {
            rotate.resetSpeed();
            rotate.max_speed = 0.2f;
            rotate.setTarget(50, 180, 0);
        }
    }
}
